package com.whnk.web.controller.pm;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.whnk.common.annotation.Log;
import com.whnk.common.base.AjaxResult;
import com.whnk.common.config.Global;
import com.whnk.common.enums.BusinessType;
import com.whnk.framework.web.base.BaseController;
import com.whnk.pm.dto.ErrorDto;
import com.whnk.pm.service.IEmpInfoImportService;
import com.whnk.pm.util.ValidateException;


@Controller
@RequestMapping("/pm/import")
public class EmpInfoImportController extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(EmpInfoImportController.class);
	@Autowired
	private IEmpInfoImportService empInfoImportService;
	
	@Log(title = "Excel导入简历", businessType = BusinessType.UPDATE)
    @PostMapping("/baseinfo")
    @ResponseBody
	public AjaxResult empInfoImport(@RequestParam("excelFile") MultipartFile file) throws Exception {
		try {
			if(!file.isEmpty()) {
				String uploadFileName = file.getOriginalFilename();
				//基本检查......
				int size = (int)file.getSize();
				//保存文件
				File desc = getAbsoluteFile(Global.getProfile(), uploadFileName);
				file.transferTo(desc);
				List<ErrorDto> msg =  new ArrayList<ErrorDto>();
				Boolean flag = false;
				try {
					
					empInfoImportService.importExcel(desc.getAbsolutePath(),msg);
				}catch(Throwable throwable) {
					if (throwable instanceof ValidateException) {
						if(msg.size() ==0) {
							return success();
						}else {
							StringBuffer buffer = new StringBuffer();
							for(ErrorDto errorDto:msg) {
								buffer.append(errorDto.toString()).append("<br>");
							}
							log.info(buffer.toString());
							return error(buffer.toString());
						}
					}else {
						return error("导入失败");
					}
				}
				return success();
			}
			return error();
		}catch(Exception e) {
			 log.error("导入失败！", e);
			return error(e.getMessage());
		}
	}
	
	 private static final File getAbsoluteFile(String uploadDir, String filename) throws IOException
	    {
	        File desc = new File(uploadDir+ File.separator + "upload" +File.separator + filename);

	        if (!desc.getParentFile().exists())
	        {
	            desc.getParentFile().mkdirs();
	        }
	        if (!desc.exists())
	        {
	            desc.createNewFile();
	        }
	        return desc;
	    }
	 
	 

		@Log(title = "Excel导入简历", businessType = BusinessType.UPDATE)
	    @PostMapping("/attrinfo")
	    @ResponseBody
		public AjaxResult empInfoImportAttr(@RequestParam("imgFile") MultipartFile file) throws Exception {
			List<String> msg = new ArrayList<>();
			try {
				if (!file.isEmpty()) {
					String uploadFileName = file.getOriginalFilename();
					//基本检查......
					int size = (int) file.getSize();
					//保存文件
					File desc = getAbsoluteFile(Global.getProfile(), uploadFileName);
					file.transferTo(desc);
					empInfoImportService.importImages(desc.getAbsolutePath(), msg);
					return success();
				}
				return error();
			} catch (Exception e) {
				if (e instanceof ValidateException) {
					if (msg.size() == 0) {
						return success();
					} else {
						StringBuffer buffer = new StringBuffer();
						for (String error : msg) {
							buffer.append(error).append("<br>");
						}
						log.info(buffer.toString());
						return error(buffer.toString());
					}
				}
				log.error("导入失败！", e);
				return error(e.getMessage());
			}
		}

	@Log(title = "Excel导入项目经历", businessType = BusinessType.UPDATE)
	@PostMapping("/projectinfo")
	@ResponseBody
	public AjaxResult projInfoImport(@RequestParam("projectExcelFile") MultipartFile file) throws Exception {
		try {
			if(!file.isEmpty()) {
				List<ErrorDto> msg =  new ArrayList<ErrorDto>();
				Boolean flag = false;
				try {
					empInfoImportService.importExcelProj(file,msg);
				}catch(Throwable throwable) {
					if (throwable instanceof ValidateException) {
						if(msg.size() ==0) {
							return success();
						}else {
							StringBuffer buffer = new StringBuffer();
							for(ErrorDto errorDto:msg) {
								buffer.append(errorDto.toString()).append("<br>");
							}
							log.info(buffer.toString());
							return error(buffer.toString());
						}
					}else {
						return error("导入失败");
					}
				}
				return success();
			}
			return error();
		}catch(Exception e) {
			log.error("导入失败！", e);
			return error(e.getMessage());
		}
	}
}
